package com.treasure.mall.biz.remote.live;

import com.treasure.mall.biz.dto.live.LiveMessageUserLevel;
import com.treasure.mall.zbq.common.MathUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 *
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.service.core.live.impl
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/11 20:42
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.biz.service.core.live.impl Inc. All rights reserved.
 */
public class TencentTools {
    private static final char[] DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String getSafeUrl(String key, String streamName, LocalDateTime time) throws NoSuchAlgorithmException {
        return getSafeUrl(key, streamName, Instant.from(time.atZone(ZoneId.systemDefault())));
    }

    private static String getSafeUrl(String key, String streamName, Instant time) throws NoSuchAlgorithmException {
        return getSafeUrl(key, streamName, Date.from(time).getTime() / 1000);
    }

    /*
     * KEY+ streamName + txTime
     */
    private static String getSafeUrl(String key, String streamName, long txTime) throws NoSuchAlgorithmException {
        String input = key +
                streamName +
                Long.toHexString(txTime).toUpperCase();

        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        String txSecret = byteArrayToHexString(
                messageDigest.digest(input.getBytes(StandardCharsets.UTF_8)));

        return "txSecret=" + txSecret + "&" + "txTime=" + Long.toHexString(txTime).toUpperCase();
    }

    private static String byteArrayToHexString(byte[] data) {
        char[] out = new char[data.length << 1];

        for (int i = 0, j = 0; i < data.length; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return new String(out);
    }

    /**
     * 获取消息用户的等级
     *
     * @param validOrderSum 用户的有效交易额
     * @return
     */
    public static LiveMessageUserLevel getMessageUserLevel(BigDecimal validOrderSum) {

        String[] names = {"少侠", "游侠", "大侠", "新贵", "豪杰", "高手", "长老", "盟主", "宗师", "归隐"};
        LiveMessageUserLevel userLevel = new LiveMessageUserLevel();

        userLevel.setLevel(0);
        userLevel.setName("少侠");
        if (validOrderSum == null || MathUtils.equalTo(validOrderSum, BigDecimal.ZERO)) {
            return userLevel;
        }
        long number = validOrderSum.divide(BigDecimal.valueOf(10), 0, RoundingMode.DOWN).longValue();
        int index = 0;
        if (number > 5 && number <= 20) {
            index = 1;
        }
        if (number > 20 && number <= 50) {
            index = 2;
        }
        if (number > 50 && number <= 200) {
            index = 3;
        }

        if (number > 200 && number <= 500) {
            index = 4;
        }

        if (number > 500 && number <= 2000) {
            index = 5;
        }
        if (number > 2000 && number <= 10000) {
            index = 6;
        }
        if (number > 10000 && number <= 100000) {
            index = 7;
        }
        if (number > 100000 && number <= 500000) {
            index = 8;
        }
        if (number > 500000) {
            index = 9;
        }
        userLevel.setLevel(index);
        userLevel.setName(names[index]);
        return userLevel;
    }

}
